package model;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.text.DateFormat;
import java.text.SimpleDateFormat;



//import Data.Patient;

/**
 * This Query Class must be used with a localhost with the appropriate database.
 * I have the database on my localhost and will demo the code. As a result, the
 * code will not work as it needs a database which you may not have and we do
 * not know how to send to you. If you like you can install xampp (windows) mamp
 * (mac) and it will set up a localhost complete with phpmyadmin where the
 * program can be tested.
 * 
 * @author Daranhdara Hun
 * @version 1.0
 */
public class MyQuery {

	/**
	 * ------------------------------------------------------------------------
	 * -------------------------------------------- Use this method to search
	 * for a patient by full name It will return a very long string with all
	 * user attribute in the following order 1 = password 9 = name 2 = username
	 * 10 = address 3 = pharmacy 11 = email 4 = birthdate 12 = suspend 5 =
	 * cellphone 13 = ssn 6 = phone 7 = insurance 8 = gender After all 13
	 * attributes are completed, the next 13 will be the attributes of the next
	 * patient matching the name criteria.
	 * 
	 * @param name - patients name
	 * @return ArrayList<User> - returns Arraylist of users
	 * @throws Exception
	 */
	public List<User> searchUserByName(String name) 
	{
		try
		{
			final PreparedStatement statement = connect().prepareStatement(
					"SELECT * FROM `patient` WHERE `name` LIKE " + "'" + name
					+ "' LIMIT 0 , 30");
			//		System.out.println("searchUserByName:line50 "+"SELECT * FROM `patient` WHERE `name` LIKE " + "'" + name
			//					+ "' LIMIT 0 , 30");
			// Creating a variable to execute query
			final ResultSet result = statement.executeQuery();
			// System.out.println(result);
			// constructs a new empty ArrayList

			final List<User> patientInfo = new ArrayList<User>();

			while (result.next()) {

				User tempUser = createUserBySSN(result.getInt(13));
				patientInfo.add(tempUser);

				/*
				 * getString returns the data. Below are patient attributes. 1 =
				 * password 9 = name 2 = username 10 = address 3 = pharmacy 11 =
				 * email 4 = birthdate 12 = suspend 5 = cellphone 13 = ssn 6 = phone
				 * 7 = insurance 8 = gender
				 */
			}

			return patientInfo;
		}
		catch(Exception e)
		{
			System.out.println("searchUserByName failled");
			return null;
		}
	}

	/**
	 * Creates connection to Database
	 * 
	 * @return
	 * @throws Exception
	 */
	private static Connection connect() 
	{
		try
		{
			// Accessing driver from the JAR file
			Class.forName("com.mysql.jdbc.Driver");

			// Creating a variable for the connection called "con"
			final Connection con = DriverManager.getConnection(
					"jdbc:mysql://localhost:8889/CS2340", "root", "root");
			// jdbc:mysql://localhost:8889/testdb --> database
			// DB user --> root
			// DB password --> root

			// insertStatement.executeUpdate("INSERT INTO names " +
			// "VALUES ('Jimmy', 'johns')");
			return con;
		}
		catch (Exception e)
		{
			System.out.println("Exception at line 103 of Query Class");
			return null;
		}
	}

	/**
	 * ------------------------------------------------------------------------
	 * -------------------------------------------- STATIC METHOD -- Will
	 * deliver a string of attributes in order from database
	 * 
	 * @param ssn - social security number of a patient needed to access the
	 *            database
	 * @param attributeNumber - the index of the attribute you want to retrieve.
	 * @return patientInfo - a string of all attributes for a particular patient
	 * @throws Exception
	 */
	private static String getUserAttributes(int ssn, int attributeNumber)
	{
		String patientInfo = "";
		try
		{
			// System.out.println("Ran getUserAttributes");
			// create the query
			final PreparedStatement statement = connect().prepareStatement(
					"SELECT * FROM patient WHERE ssn=" + ssn);

			// Creating a variable to execute query
			final ResultSet result = statement.executeQuery();



			while (result.next()) {
				switch (result.getMetaData().getColumnType(attributeNumber)) {
				case java.sql.Types.DATE:

					DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

					// System.out.println("TESTING: " +
					// result.getDate(attributeNumber));
					patientInfo = formatter.format(result.getDate(attributeNumber));
					// System.out.println("Date: " + patientInfo);
					break;

				default:
					patientInfo = result.getString(attributeNumber);
					break;
				}
				// DateFormat formatter;
				// formatter = new SimpleDateFormat("yyyy-MM-dd");
				// System.out.println("Check a4:Before try " +formatter);
				/*
				 * patientInfo= result.getString(1) + " " + result.getString(2) +
				 * " " + result.getString(3) + " " + result.getString(4)+ " " +
				 * result.getString(5) + " " + result.getString(6) + " " +
				 * result.getString(7) + " " + result.getString(8)+ " " +
				 * result.getString(9) + " " + result.getString(10) + " " +
				 * result.getString(11) + " " + result.getString(12)+ " " +
				 * result.getString(13); getString returns the data. Below are
				 * patient attributes. 1 = password 9 = name 2 = username 10 =
				 * address 3 = pharmacy 11 = email 4 = birthdate 12 = suspend 5 =
				 * cellphone 13 = ssn 6 = phone 7 = insurance 8 = gender
				 */
			}
			return patientInfo;
		}
		catch(Exception e)
		{
			System.out.println("Exception: Returning emtpy string at MyQuery:getUserAttributes");
			return patientInfo;
		}
	}

	/**
	 * ------------------------------------------------------------------------
	 * -------------------------------------------- Use this method ONLY if
	 * you're accessing an attribute with a DATE DATATYPE.
	 * 
	 * @param ssn - Patient social security number
	 * @param attributeNumber - number corresponding to the attribute number
	 * 			of a patient from the database
	 *		  Below are patient attributes: 
	 *		  1 = password 9 = name 2 = username 10 = address 3 = pharmacy 11 =
	 *		  email 4 = birthdate 12 = suspend 5 = cellphone 13 = ssn 6 = phone
	 *		  7 = insurance 8 = gender
	 *
	 * @return
	 * @throws Exception
	 */
	private Date getPatientBday(int ssn, int attributeNumber) 
	{
		Date patientInfo = null;
		try
		{
			// System.out.println("Ran getPatientBday");
			// create the query
			final PreparedStatement statement = connect().prepareStatement(
					"SELECT * FROM patient WHERE ssn=" + ssn);

			// Creating a variable to execute query
			final ResultSet result = statement.executeQuery();

			while (result.next()) {
				switch (result.getMetaData().getColumnType(attributeNumber)) {
				case java.sql.Types.DATE:
					patientInfo = result.getDate(attributeNumber);
					break;
				default:
					patientInfo = null;
					break;
				}

			}
			return patientInfo;
		}
		catch(SQLException s)
		{
			System.out.println("SQL Exception at MyQuery: getPatientBday and will return a null date");
			return patientInfo;
		}
		finally
		{
			System.out.println("SQL Exception at MyQuery: getPatientBday");
		}
	}

	/**
	 * ------------------------------------------------------------------------
	 * -------------------------------------------- Saves Patient information
	 * into database for the first time Employee will call this method to save
	 * patient information
	 * 
	 * @param User 
	 * 			- Employee must provide social security number
	 * @throws Exception
	 */
	public void addUser(User user) {
		// System.out.println("Ran addUser");
		Statement stmt = null;

		/**/
		final String password = user.getPassword();
		final String username = user.getUserName();
		final String pharmacy = user.getPharmacy();
		Date birthdate = user.getBirthDate();
		final Date date = new Date(1333333333);
		if (birthdate == null) {
			birthdate = date;
		}

		final String cellphone = user.getCellPhone();
		final String phone = user.getPhone();
		final String insurance = user.getInsurance();
		final Gender gender = user.getGender();
		final String name = user.getName();
		final String address = user.getAddress();
		final String email = user.getEmail();
		final boolean suspended = user.getSuspended();
		final int ssn = user.getSSN();
		/*
		 * INSERT INTO `CS2340`.`patient` (`password`, `username`, `pharmacy`,
		 * `birthdate`, `cellphone`, `phone`, `insurance`, `gender`, `name`,
		 * `address`, `email`, `suspended`, `ssn`) VALUES ('c', 'd', NULL, NULL,
		 * NULL, NULL, NULL, 'N/A', '', NULL, '', 'true', '');
		 */


		try {
			stmt = connect().createStatement();

			stmt.executeUpdate("INSERT INTO `CS2340`.`patient` "
					+ "(`password`, `username`, `pharmacy`, `birthdate`, " +
					"`cellphone`, `phone`, `insurance`, `gender`, `name`," +
					" `address`, `email`, `suspended`, `ssn`) "
					+ "VALUES " + "('"
					+ password
					+ "', '"
					+ username
					+ "', '"
					+ pharmacy
					+ "', '"
					+ birthdate
					+ "', '"
					+ cellphone
					+ "', '"
					+ phone
					+ "', '"
					+ insurance
					+ "', '"
					+ gender
					+ "', '"
					+ name
					+ "', '"
					+ address
					+ "', '"
					+ email
					+ "', '"
					+ suspended
					+ "', '"
					+ ssn + "');");

			/*
			 * getString returns the data. Below are patient attributes. 1 =
			 * password 9 = name 2 = username 10 = address 3 = pharmacy 11 =
			 * email 4 = birthdate 12 = suspend 5 = cellphone 13 = ssn 6 = phone
			 * 7 = insurance 8 = gender
			 */
		} catch (SQLException e) {
			System.out.println("SQLException problem");
		} catch (Exception e) {
			System.out.println("Exception problem");
			// TODO Auto-generated catch block
			// System.out.println("test");
			// e.printStackTrace();
		}

	}

	/**
	 * ------------------------------------------------------------------------
	 * -------------------------------------------- Use this method to update a
	 * users information
	 * 
	 * @param ssn
	 *            - must use SSN number when using this method
	 * @throws SQLException
	 * @throws Exception
	 */
	public void updateUser(User user, int ssn) 
	{

		// System.out.println("Ran updateUser");
		Statement stmt = null;

		/**/
		final String password = user.getPassword();
		// String username = user.getUserName();
		final String pharmacy = user.getPharmacy();
		final Date birthdate = user.getBirthDate();
		final String cellphone = user.getCellPhone();
		final String phone = user.getPhone();
		final String insurance = user.getInsurance();
		final Gender gender = user.getGender();
		final String name = user.getName();
		final String address = user.getAddress();
		final String email = user.getEmail();
		// Boolean suspended = user.getSuspended();

		/*
		 * UPDATE `CS2340`.`patient` SET `password` = 'testfgfg', `username` =
		 * 'testfg', `pharmacy` = 'testfg', `cellphone` = 'fg', `phone` = 'fg',
		 * `insurance` = 'teser', `gender` = 'FEMALE', `name` = 'testgfg',
		 * `address` = 'testfg', `email` = 'testggf', `suspended` = '0',
		 * 
		 * Table Attribute List: getString returns the data. Below are patient
		 * attributes. 1 = password 9 = name 2 = username 10 = address 3 =
		 * pharmacy 11 = email 4 = birthdate 12 = suspend 5 = cellphone 13 = ssn
		 * 6 = phone 7 = insurance 8 = gender
		 * 
		 * UPDATE `CS2340`.`patient` SET `password` = 'cu', `username` = 'uu',
		 * `pharmacy` = 'u', `birthdate` = '2011-03-14', `cellphone` = 'qw',
		 * `phone` = 'qw', `insurance` = 'qw', `gender` = 'Female', `name` =
		 * 'qw', `address` = 'qw', `email` = 'rrrrrrr' WHERE `patient`.`ssn` = 0
		 * 
		 * "UPDATE `CS2340`.`patient` SET " + " `password` = '"+password+"', " +
		 * "`username` = '"+username+"', " + "`pharmacy` = '"+pharmacy+"', " +
		 * "`birthdate` = '"+bi+"', " + "`cellphone` = '"+cellphone+"', " +
		 * "`phone` = '"+phone+"', " + "`insurance` = '"+insurance+"', " +
		 * "`gender` = '"+gender+"', " + "`name` = '"+name+"', " +
		 * "`address` = '"+address+"`, " + "`email` = '"+email+"', " +
		 * "`suspended` = '"+suspended+"', " +
		 * "`ssn` = '"+ssn+"' WHERE `patient`.`ssn` ="+ssn
		 */
		try 
		{
			stmt = connect().createStatement();
			stmt.executeUpdate("UPDATE  `CS2340`.`patient` SET  "
					+ "`password` =  '"
					+ password
					+ "', "
					+ "`username` =  '"
					+ password
					+ "', "
					+ "`pharmacy` =  '"
					+ pharmacy
					+ "',"
					+ "`birthdate` =  '"
					+ birthdate
					+ "',"
					+ "`cellphone` =  '"
					+ cellphone
					+ "',"
					+ "`phone` =  '"
					+ phone
					+ "',"
					+ "`insurance` =  '"
					+ insurance
					+ "',"
					+ "`gender` =  '"
					+ gender
					+ "',"
					+ "`name` =  '"
					+ name
					+ "',"
					+ "`address` =  '"
					+ address
					+ "',"
					+ "`email` =  '"
					+ email
					+ "'"
					+ "WHERE `patient`.`ssn` = " + ssn);
		} 
		catch(SQLException s)
		{
			System.out.println("SQLException at MySql: Update User");
		}
		finally 
		{
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}


	/**
	 * ------------------------------------------------------------------------
	 * -------------------------------------------- STATIC METHOD -- Deletes a
	 * user never to return again.
	 * 
	 * @param ssn
	 *            - must specify user by SSN
	 * @throws SQLException
	 * @throws Exception
	 */
	public void deleteUser(int ssn)
	{
		// System.out.println("Ran deleteUser");
		Statement stmt = null;

		try 
		{
			stmt = connect().createStatement();
			/*
			 * DELETE FROM `CS2340`.`patient` WHERE `patient`.`ssn` = 123456789
			 * SELECT * FROM `patient` WHERE 1 DELETE FROM `CS2340`.`patient` WHERE
			 * `patient`.`ssn` = 234540987
			 */
			final PreparedStatement test = connect().prepareStatement("SELECT * FROM `CS2340`.`patient` WHERE `patient`.`ssn` = "
					+ ssn);
			final ResultSet result = test.executeQuery();

			if (result.next())
			{
				try 
				{
					stmt.executeUpdate("DELETE FROM `CS2340`.`patient` WHERE `patient`.`ssn` = " + ssn);

				} 
				finally 
				{
					stmt.close();
				}
			}
		}
		catch (SQLException s)
		{
			System.out.println("SQLException at MySql: deleteUser");
		}
	}

	/**
	 * ------------------------------------------------------------------------
	 * --------------------------------------------
	 * Given a username and password, this method will check with the database
	 * if there is a match, a user will be returned
	 * @param password - Patient password in String format
	 * @param username - Patient username in String format
	 * @return User - User class with all variables gathered from database
	 * 
	 */
	public User authenticateUser(String username, String password)
	{
		// System.out.println("Ran authenticateUser");
		/* create the query */
		try
		{
			final PreparedStatement statement = connect().prepareStatement(
					"SELECT * FROM `patient` WHERE `username` LIKE '" + username
					+ "'");

			/* Creating a variable to execute query */
			final ResultSet result = statement.executeQuery();

			String dbPassword = "";

			while (result.next()) {
				dbPassword = result.getString(1);

				/*
				 * getString returns the data. Below are patient attributes. 1 =
				 * password 9 = name 2 = username 10 = address 3 = pharmacy 11 =
				 * email 4 = birthdate 12 = suspend 5 = cellphone 13 = ssn 6 = phone
				 * 7 = insurance 8 = gender
				 */
			}

			/* Create User */

			if (password.equals(dbPassword)) /* Password Success */{
				// System.out.println("Query authenticateUser -- Login Successful");
				return createUser(username);
			} else /* Password Fail */{
				// System.out.println("Query authenticateUser -- Login Failed -- returning 'No User' user");
				return null;
				/*return createUser("No User");*/
			}
		}
		catch(Exception e)
		{
			System.out.println("SQLException at MySql: authenticateUser");
			return null;
		}
	}

	/**
	 * Parses a string of zeros and ones and converts each parsed item into a boolean. 
	 * The converted item is then stored in an array
	 * @param role - String role in coded format (1000=patient, 0100=nurse, 0010=doctor, 0001=admin)
	 * @return boolean[] - array of booleans elements with length of 4
	 */
	public boolean[] convertToRoleArray(String role)
	{
		boolean[] boolArr = new boolean[4];
		Scanner scan = new Scanner(role);
		int i = 0;
		while(scan.hasNext())
		{
			if(scan.next() == "0")
			{
				boolArr[i] = false;
			}
			else boolArr[i] = true;
			i++;
		}
		return boolArr;
	}	

	/**
	 * Use this method only after the authenticateUser method has passed 
	 * @param user
	 * @param role (0 = patient, 1 = nurse, 2 = doctor, 3 = admin)
	 * @return "null" if authenticateRole failed. An actual user is returned if 
	 * 				this method is successful; 
	 * 
	 */
	public User authenticateRole(User user, String role)
	{
		try{

			// System.out.println("Ran authenticateUser");
			/* create the query */

			final PreparedStatement statement = connect().prepareStatement(
					"SELECT * FROM `roles` WHERE `ssn`='" +user.getSSN()+ "'");

			/* Creating a variable to execute query */
			final ResultSet result = statement.executeQuery();
			int rolefound = 0;
			while (result.next()) 
			{
				if (result.getString(2) == role)
				{
					user.setRoles(convertToRoleArray(role));
					rolefound = 1;
				}

				/*
				 * 1 = ssn and 2=role
				 */
			}

			if (rolefound == 1)
			{
				return user;
			}
			else return null;
		}
		catch(Exception e)
		{
			System.out.println("Exception at MyQuery:authenticateRole");
			return null;
		}
	}

	/**
	 * ------------------------------------------------------------------------
	 * -------------------------------------------- Use this method to return a
	 * user matching the SSN provided
	 * 
	 * @param ssn
	 * @return User -- The User class will be returned with all its attributes
	 *         gathered from the database
	 * 
	 */

	public User loadUser(int ssn) 
	{
		User tempuser = null;
		// System.out.println("Ran loadUser");
		try
		{
			final PreparedStatement test = connect().prepareStatement("SELECT * FROM `CS2340`.`patient` WHERE `ssn`='" + ssn + "'");
			final ResultSet result = test.executeQuery();

			while (result.next())
			{
				final PreparedStatement statement = connect().prepareStatement(
						"SELECT * FROM `CS2340`.`patient` WHERE `ssn` LIKE '" + ssn + "'");

				/* Creating a variable to execute query */
				final ResultSet results = statement.executeQuery();
				int temp=0;

				while (results.next()) 
				{
					temp = result.getInt(13);
					System.out.println(temp);

					/*
					 * getString returns the data. Below are patient attributes. 1 =
					 * password 9 = name 2 = username 10 = address 3 = pharmacy 11 =
					 * email 4 = birthdate 12 = suspend 5 = cellphone 13 = ssn 6 = phone
					 * 7 = insurance 8 = gender
					 */
				}
				tempuser = createUserBySSN(temp);

				/*Adds all treatment records to the user class*/
				final PreparedStatement anothertest = connect().prepareStatement("SELECT * FROM `CS2340`.`treatment_record` WHERE `ssn`='" + ssn + "'");
				final ResultSet anotherresult = anothertest.executeQuery();
				while (anotherresult.next())
				{
					int trID = Integer.parseInt(anotherresult.getString(14));
					System.out.println("Line 593:" + trID );
					TreatmentRecord temptr = createTreatmentRecord(trID);
					System.out.println("Line 595:" + temptr );
					tempuser.addTreatmentRecord(temptr);
					System.out.println("Line 597:" + tempuser );
				}
				return tempuser;
			}	
		}
		catch(SQLException e) 
		{
			System.out.println("Exception at line 593 of Query Class");
			return null;
		}
		return tempuser;


	}

	/**
	 * ------------------------------------------------------------------------
	 * -------------------------------------------- You must specify the
	 * username first. Usernames are unique
	 * 
	 * @param username
	 * @return int -- this will be the patients SSN with no dashes or spaces
	 *         between the numbers
	 * 
	 */
	public int getSSN(String username)
	{
		// System.out.println("Ran getSSN");
		// create the query
		try
		{
			final PreparedStatement statement = connect().prepareStatement(
					"SELECT * FROM `patient` WHERE `username` LIKE '" + username
					+ "'");

			// Creating a variable to execute query
			final ResultSet result = statement.executeQuery();

			int socialNum = 0;

			while (result.next()) {
				socialNum = Integer.parseInt(result.getString(13));
				/*
				 * getString returns the data. Below are patient attributes. 1 =
				 * password 9 = name 2 = username 10 = address 3 = pharmacy 11 =
				 * email 4 = birthdate 12 = suspend 5 = cellphone 13 = ssn 6 = phone
				 * 7 = insurance 8 = gender
				 */

			}
			return socialNum;
		}
		catch(SQLException e)
		{
			System.out.println("getSSN SQL Exception");
			return (Integer) null;
		}

	}

	/**
	 * ------------------------------------------------------------------------
	 * -------------------------------------------- Creates a User object. Use
	 * this method when accessing User Information from the database A Nurse or
	 * Doctor will use this method whenever he pulls up information on a patient
	 * 
	 * @param username
	 * @return User
	 *
	 */
	private User createUser(String username) 
	{
		final User user = new User();
		try{
			// System.out.println("Ran createUser");

			int tempSSN = 0;

			tempSSN = getSSN(username);

			final String a1 = getUserAttributes(tempSSN,1);
			final String a2 = getUserAttributes(tempSSN,2);
			final String a3 = getUserAttributes(tempSSN,3);
			final Date a4 = getPatientBday(tempSSN,4);
			final String a5 = getUserAttributes(tempSSN,5);
			final String a6 = getUserAttributes(tempSSN,6);
			final String a7 = getUserAttributes(tempSSN,7);
			final String a8 = getUserAttributes(tempSSN,8);
			final String a9 = getUserAttributes(tempSSN,9);
			final String a10 = getUserAttributes(tempSSN,10);
			final String a11 = getUserAttributes(tempSSN,11);
			final String a12 = getUserAttributes(tempSSN,12);
			final String a13 = getUserAttributes(tempSSN,13);
			//System.out.println("myQuery(559): SSN: " + a13);
			user.setPassword(a1);
			user.setUserName(a2);
			user.setPharmacy(a3);

			user.setBirthDate(a4);
			user.setCellPhone(a5);
			user.setPhone(a6);
			user.setInsurance(a7);
			/* Convert String enum to true java Enum */
			/* System.out.print("print a8: " +a8); */
			if (a8.equals("Male")) {
				user.setGender(Gender.MALE);
			} else{
				user.setGender(Gender.FEMALE);
			}

			user.setName(a9);
			user.setAddress(a10);
			user.setEmail(a11);

			user.setSuspended(convertStringToBoolean(a12));

			user.setSSN(Integer.parseInt(a13));
			System.out.println("CreateUser Method of Query Class: " + user);
			return user;
			/*
			 * getString returns the data. Below are patient attributes. 1 =
			 * password 9 = name 2 = username 10 = address 3 = pharmacy 11 = email 4
			 * = birthdate 12 = suspend 5 = cellphone 13 = ssn 6 = phone 7 =
			 * insurance 8 = gender
			 */
		}
		catch(Exception e)
		{
			System.out.println("Exception in CreateUser in Query Class");
			return user;
		}
	}

	/**
	 * Call this method to if you have the patients ssn number to retrieve a user class
	 * @param ssn - patient social security number in integer form
	 * @return User - return 
	 * 
	 */
	private User createUserBySSN(int ssn) 
	{

		// System.out.println("Ran createUser");
		final User user = new User();
		int tempSSN = 0;

		tempSSN = ssn;

		final String a1 = getUserAttributes(tempSSN, 1);
		final String a2 = getUserAttributes(tempSSN, 2);
		final String a3 = getUserAttributes(tempSSN, 3);
		final Date a4 = getPatientBday(tempSSN, 4);
		final String a5 = getUserAttributes(tempSSN, 5);
		final String a6 = getUserAttributes(tempSSN, 6);
		final String a7 = getUserAttributes(tempSSN, 7);
		final String a8 = getUserAttributes(tempSSN, 8);
		final String a9 = getUserAttributes(tempSSN, 9);
		final String a10 = getUserAttributes(tempSSN, 10);
		final String a11 = getUserAttributes(tempSSN, 11);
		final String a12 = getUserAttributes(tempSSN, 12);
		final String a13 = getUserAttributes(tempSSN, 13);

		user.setPassword(a1);
		user.setUserName(a2);
		user.setPharmacy(a3);

		user.setBirthDate(a4);
		user.setCellPhone(a5);
		user.setPhone(a6);
		user.setInsurance(a7);
		/* Convert String enum to true java Enum */
		/* System.out.print("print a8: " +a8); */
		if (a8.equals("Male")) 
		{
			user.setGender(Gender.MALE);
		} 
		else
		{
			user.setGender(Gender.FEMALE);
		}

		user.setName(a9);
		user.setAddress(a10);
		user.setEmail(a11);

		user.setSuspended(convertStringToBoolean(a12));
		// System.out.println("If SSN is '111111111', then user login information failed: SSN is "
		// + a13);
		//System.out.println("Username: " + username + " " + a9);
		System.out.println("Social Security: " + a13);
		user.setSSN(Integer.parseInt(a13));
		return user;

		/*
		 * getString returns the data. Below are patient attributes. 1 =
		 * password 9 = name 2 = username 10 = address 3 = pharmacy 11 = email 4
		 * = birthdate 12 = suspend 5 = cellphone 13 = ssn 6 = phone 7 =
		 * insurance 8 = gender
		 */

	}

	/**
	 * Will return a string of patient attributes space delimited. 
	 * @param ssn - 
	 * @param attributeNumber
	 * @return
	 *
	 */
	private static String getAppAttributes(int ssn, int attributeNumber)
	{
		try{
			// System.out.println("Ran getAppAttributes");
			// create the query
			final PreparedStatement statement = connect().prepareStatement(
					"SELECT * FROM appointment WHERE ssn=" + ssn);

			// Creating a variable to execute query
			final ResultSet result = statement.executeQuery();

			String appInfo = "";

			while (result.next()) {
				switch (result.getMetaData().getColumnType(attributeNumber)) {
				case java.sql.Types.DATE:

					DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

					// System.out.println("TESTING: " +
					// result.getDate(attributeNumber));
					appInfo = formatter.format(result.getDate(attributeNumber));
					// System.out.println("Date: " + appInfo);break;
					break;
				default:
					appInfo = result.getString(attributeNumber);
				}
			}
			return appInfo;
		}
		catch(Exception e)
		{
			System.out.println("Exception at MyQuery: getAppAttributes");
			return null;
		}
	}

	/**
	 * ------------------------------------------------------------------------
	 * -------------------------------------------- String to boolean conversion
	 */
	private boolean convertStringToBoolean(String booleanString) {
		// System.out.println("Ran convertStringToBoolean");
		final String strBoolean = booleanString;

		/* Do the String to boolean conversion */
		final boolean theValue = Boolean.parseBoolean(strBoolean);

		return theValue;
	}

	/**
	 * ------------------------------------------------------------------------
	 * --------------------------------------------
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) 
	{
		try 
		{

			//		User steve1;
			//		final Date date = new Date(20110409);
			//
			//final boolean[] arr = { true, false, false, false };
			MyQuery newquery = new MyQuery();
			int ssn = 7;
			User newUser = newquery.loadUser(ssn);
			//PatientInformation pi = newUser.getPatientInfo();
			//List<TreatmentRecord> tr = pi.getTreatmentRecords();
			System.out.println ("Line 850:" + newUser);
			System.out.println
			(
					"Password: " + newUser.getPassword() + "\n" +
					"Username: " + newUser.getUserName() + "\n" +
					"Pharmacy: " + newUser.getPharmacy() + "\n" +
					"birthdate: " + newUser.getBirthDate() + "\n" +
					"cellphone: " + newUser.getCellPhone() + "\n" +
					"phone: " + newUser.getPhone() + "\n" +
					"insurance" + newUser.getInsurance() + "\n" +
					"gender" + newUser.getGender() + "\n" +
					"Name" + newUser.getName() + "\n" +
					"Address" + newUser.getAddress() + "\n" +
					"email" + newUser.getEmail() + "\n" +
					"suspended" + newUser.getSuspended() + "\n" +
					"SSN" + newUser.getSSN() + "\n" +
					"Treatment Record Stuff: "

			);
			TreatmentRecord newtr = null;
			Iterator<TreatmentRecord> iterator = null;
			iterator = (newUser.getTreatmentRecords()).iterator();
			while(iterator.hasNext())
			{
				newtr = iterator.next();
				int count = 1;
				System.out.println
				(
						"TreatmentRecord #" + count + ": " +
						"Diagnosis: " + newtr.getDiagnosis() + "\n" +
						"Temperature: " + newtr.getTemperature() + "\n" +
						"Height: " + newtr.getHeight() + "\n" +
						"Time: " + newtr.getTime() + "\n" +
						"Nurse: " + newtr.getAttendingNurse() + "\n" +
						"Symptoms: " + newtr.getSymptoms() + "\n" +
						"diastolic: " + newtr.getBpDiastolic() + "\n" +
						"systolic: " + newtr.getBpSystolic() + "\n" +
						"doctor: " + newtr.getTreatingDoctor() + "\n" +
						"pulse: " + newtr.getPulse() + "\n" +
						"weight: " + newtr.getWeight() + "\n" +
						"date: " + newtr.getDate() + "\n" 				
				);	
				count++;

			}

		}
		catch(Exception e)
		{
			System.out.println("Main Failed");

		}

	}

	// Save Apppointments and Patient Invoices
	/*
	 * BOOLEAN delete appointment() Apppointment loadAppointment() boolean
	 * addAppointment(Appointments app) // searchUserAppointments boolean
	 * updateAppointment(Appointment app)
	 */
	/**
	 * ------------------------------------------------------------------------
	 * -----------------------------Need to be tested for Time Use this method
	 * ONLY if you're accessing an attribute with a DATE DATATYPE.
	 * 
	 * @param ssn
	 * @param attributeNumber
	 * @return Date
	 * @throws Exception
	 */
	private Date getAppDate(int ssn, int attributeNumber) 
	{
		try{
			//System.out.println("Ran getAppDate");
			// create the query
			final PreparedStatement statement = connect().prepareStatement(
					"SELECT * FROM appointment WHERE ssn=" + ssn);

			// Creating a variable to execute query
			final ResultSet result = statement.executeQuery();

			Date date = null;

			while (result.next()) {
				switch (result.getMetaData().getColumnType(attributeNumber)) {
				case java.sql.Types.DATE:
					date = result.getDate(attributeNumber);
					break;
				default:
					date = null;
					break;
				}

			}
			return date;
		}
		catch(Exception e)
		{
			System.out.println("Exception at MyQuery: getAppDate");
			return null;
		}
	}

	/**
	 * ------------------------------------------------------------------------
	 * -----------------------------Need to be tested for Time Use this to
	 * access a particular appointment
	 * 
	 * @param ssn
	 * @param date
	 * @param user
	 * @return Appointment
	 * @throws SQLException
	 * @throws Exception
	 */
	public Appointment loadAppointment(int ssn, Date date)  
	{
		try
		{

			// System.out.println("Ran loadAppointment");
			/*
			 * SELECT * FROM `appointment` WHERE `ssn`='111111111'AND
			 * `date`='0000-00-00'
			 */
			final PreparedStatement statement = connect().prepareStatement(
					"SELECT * FROM `appointment` WHERE `ssn` = '" + ssn
					+ "' AND `date` = '" + date + "'");

			/* Creating a variable to execute query */
			final ResultSet result = statement.executeQuery();
			String dbSSN = "";

			while (result.next()) 
			{
				dbSSN = result.getString(1);

				/*
				 * Below are appointment attributes. 1 = ssn 5 = reason 2 = date 6 =
				 * name 3 = timeStart 7 = timeEnd 4 = doctor 8 = patient
				 */
			}
			return createAppointment(dbSSN);
		}
		catch(SQLException e)
		{
			System.out.println("SQL Exception at MyQuery: loadAppointment");
			return null;
		}
	}

	/**
	 * Use this method when you have a name and need all the appointments
	 * associated with this name in an Arraylist
	 * 
	 * @param name
	 * @return ArrayList<Appointment>
	 * @throws Exception
	 */
	public List<Appointment> searchUserAppointments(int ssn)
	throws Exception {
		// System.out.println("SELECT * FROM `CS2340`.`appointment` WHERE `name` LIKE "
		// + "'"+name+"' LIMIT 0 , 30");
		// create the query
		final PreparedStatement statement = connect().prepareStatement(
				"SELECT * FROM `CS2340`.`appointment` WHERE `ssn`=" + "'"
				+ ssn + "' LIMIT 0 , 30");

		// Creating a variable to execute query
		final ResultSet result = statement.executeQuery();
		// System.out.println(result);
		// constructs a new empty ArrayList

		final List<Appointment> appInfo = new ArrayList<Appointment>();

		while (result.next()) {
			Appointment tempApp = createAppointment(result.getString(6));
			appInfo.add(tempApp);
			/*
			 * Below are appointment attributes. 1 = ssn 5 = reason 2 = date 6 =
			 * name 3 = timeStart 7 = timeEnd 4 = doctor 8 = patient
			 */
		}
		return appInfo;
	}


	public boolean addTreatmentRecord(int ssn, Date date, TreatmentRecord tr) {
		// System.out.println("Ran addAppointment");
		Statement stmt = null;

		/**/
		final String diagnosis = tr.getDiagnosis();
		final double temperature = tr.getTemperature();
		final Height height = tr.getHeight();
		final Time time = tr.getTime();
		final String nurse = tr.getAttendingNurse();
		final String symptoms = tr.getSymptoms();
		final int diastolic = tr.getBpDiastolic();
		final int systolic = tr.getBpSystolic();
		final String doctor = tr.getTreatingDoctor();
		final int pulse = tr.getPulse();
		final double weight = tr.getWeight();


		/*
		 * System.out.print("INSERT INTO `CS2340`.`treatment_record` "
					+ "(`ssn`, `diagnosis`, `temperature`, `height`, `time`,"
					+ " `attending_nurse`, `symptoms`, `bp_diastolic`, `bp_systolic`," +
							" `treating_doctor`, `pulse`, `weight`, `date`) "
					+ "VALUES " + "('" + ssn + "', '" + diagnosis + "', '"
					+ temperature + "', '" + height + "', '" + time + "', '"
					+ nurse + "', '" + symptoms + "', '" + diastolic + "', '" 
					+ systolic +"', '" + doctor + "', '" + pulse + "', '"+weight+ "', '"
					+ date + "');");
		 */

		try {
			stmt = connect().createStatement();

			stmt.executeUpdate("INSERT INTO `CS2340`.`treatment_record` "
					+ "(`ssn`, `diagnosis`, `temperature`, `height`, `time`,"
					+ " `attending_nurse`, `symptoms`, `bp_diastolic`, `bp_systolic`," +
					" `treating_doctor`, `pulse`, `weight`, `date`) "
					+ "VALUES " + "('" + ssn + "', '" + diagnosis + "', '"
					+ temperature + "', '" + height + "', '" + time + "', '"
					+ nurse + "', '" + symptoms + "', '" + diastolic + "', '" 
					+ systolic +"', '" + doctor + "', '" + pulse + "', '"+weight+ "', '"
					+ date + "')");

			/*
			 * Below are appointment attributes. 1 = ssn 5 = reason 2 = date 6 =
			 * name 3 = timeStart 7 = timeEnd 4 = doctor 8 = patient
			 */
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			e.getClass();
			// System.out.println("SQL Exception in addAppointment");
			return false;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public List<TreatmentRecord> getTreatmentRecords(int ssn)
	throws Exception {
		// System.out.println("SELECT * FROM `CS2340`.`appointment` WHERE `name` LIKE "
		// + "'"+name+"' LIMIT 0 , 30");
		// create the query
		final PreparedStatement statement = connect().prepareStatement(
				"SELECT * FROM `CS2340`.`treatment_record` WHERE `ssn`=" + "'"
				+ ssn + "' LIMIT 0 , 30");

		// Creating a variable to execute query
		final ResultSet result = statement.executeQuery();
		// System.out.println(result);
		// constructs a new empty ArrayList

		final List<TreatmentRecord> trInfo = new ArrayList<TreatmentRecord>();

		while (result.next()) {
			TreatmentRecord temptr = createTreatmentRecord(result.getInt(14));
			trInfo.add(temptr);
			/*
			 * Below are appointment attributes. 
			 * 1=ssn 
			 * 12=weight 
			 * 14=treat_recordID
			 */
		}
		return trInfo;
	}

	private static String gettrAttributes(int trID, int attributeNumber)
	throws Exception {
		// System.out.println("Ran getAppAttributes");
		// create the query
		final PreparedStatement statement = connect().prepareStatement(
				"SELECT * FROM treatment_record WHERE treat_recordID=" + trID);

		// Creating a variable to execute query
		final ResultSet result = statement.executeQuery();

		String trInfo = "";

		while (result.next()) {
			switch (result.getMetaData().getColumnType(attributeNumber)) {
			case java.sql.Types.DATE:

				DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

				// System.out.println("TESTING: " +
				// result.getDate(attributeNumber));
				trInfo = formatter.format(result.getDate(attributeNumber));
				// System.out.println("Date: " + appInfo);break;
				break;
			default:
				trInfo = result.getString(attributeNumber);
			}
		}
		return trInfo;
	}

	private Time gettrTime(int trID, int attributeNumber) throws Exception {
		// System.out.println("Ran getAppTime");
		// create the query
		final PreparedStatement statement = connect().prepareStatement(
				"SELECT * FROM treatment_record WHERE treat_recordID=" + trID);

		// Creating a variable to execute query
		final ResultSet result = statement.executeQuery();

		Time time = null;

		while (result.next()) {
			switch (result.getMetaData().getColumnType(attributeNumber)) {
			case java.sql.Types.TIME:
				time = result.getTime(attributeNumber);
				break;
			default:
				time = null;
				break;
			}

		}
		return time;
	}

	private Date gettrDate(int trID, int attributeNumber) throws Exception {
		//System.out.println("Ran getAppDate");
		// create the query
		final PreparedStatement statement = connect().prepareStatement(
				"SELECT * FROM treatment_record WHERE treat_recordID=" + trID);

		// Creating a variable to execute query
		final ResultSet result = statement.executeQuery();

		Date date = null;

		while (result.next()) {
			switch (result.getMetaData().getColumnType(attributeNumber)) {
			case java.sql.Types.DATE:
				date = result.getDate(attributeNumber);
				break;
			default:
				date = null;
				break;
			}

		}
		return date;
	}

	private TreatmentRecord createTreatmentRecord(int trID) 
	{
		try
		{
			// System.out.println("Ran createTreatmentRecord");


			final String a2 = gettrAttributes(trID, 2); /* diagnosis */
			final String a3 = gettrAttributes(trID, 3); /* temperature */
			final String a4 = gettrAttributes(trID, 4); /* height */
			final Time a5 = gettrTime(trID, 5); /* time */
			final String a6 = gettrAttributes(trID, 6); /* attending nurse */
			final String a7 = gettrAttributes(trID, 7); /* symptoms */
			final String a8 = gettrAttributes(trID, 8); /* bp_diastolic */
			final String a9 = gettrAttributes(trID, 9); /* bp_systolic */
			final String a10 = gettrAttributes(trID, 10); /* treating_doctor */
			final String a11 = gettrAttributes(trID, 11); /* pulse */
			final String a12 = gettrAttributes(trID, 12); /* weight */
			final Date a13 = gettrDate(trID, 13); /* date */

			final TreatmentRecord tr = new TreatmentRecord();
			tr.setDiagnosis(a2);
			//System.out.println("Height: "+a4);
			tr.setTemperature(Double.parseDouble(a3));
			tr.setHeight(HeightMessageEditor.getHeight(a4));
			tr.setTime(a5);
			tr.setAttendingNurse(a6);
			tr.setSymptoms(a7);
			tr.setBpDiastolic(Integer.parseInt(a8));
			tr.setBpSystolic(Integer.parseInt(a9));
			tr.setTreatingDoctor(a10);
			tr.setPulse(Integer.parseInt(a11));
			tr.setWeight(Double.parseDouble(a12));
			tr.setDate(a13);

			return tr;
		}
		catch (Exception e)
		{
			System.out.println("Exception at 1269 in Query Class");
			return null;
		}
	}

	/**
	 * Returns a list of appointments for that date
	 * @param date - date user is searching by
	 * @return List<Appointment> 
	 * @throws Exception
	 */
	public List<Appointment> searchUserAppointmentDateOnly(Date date)
	throws Exception {
		// System.out.println("SELECT * FROM `CS2340`.`appointment` WHERE `name` LIKE "
		// + "'"+name+"' LIMIT 0 , 30");
		// create the query
		final PreparedStatement statement = connect().prepareStatement(
				"SELECT * FROM `CS2340`.`appointment` WHERE `date`='" +
				date +"' LIMIT 0 , 30");

		// Creating a variable to execute query
		final ResultSet result = statement.executeQuery();
		// System.out.println(result);
		// constructs a new empty ArrayList

		final List<Appointment> appInfo = new ArrayList<Appointment>();

		while (result.next()) {
			Appointment tempApp = createAppointment(result.getString(6));
			appInfo.add(tempApp);
			/*
			 * Below are appointment attributes. 1 = ssn 5 = reason 2 = date 6 =
			 * name 3 = timeStart 7 = timeEnd 4 = doctor 8 = patient
			 */
		}
		return appInfo;
	}


	public List<Appointment> searchUserAppointmentDate(int ssn, Date date)
	throws Exception {
		// System.out.println("SELECT * FROM `CS2340`.`appointment` WHERE `name` LIKE "
		// + "'"+name+"' LIMIT 0 , 30");
		// create the query
		final PreparedStatement statement = connect().prepareStatement(
				"SELECT * FROM `CS2340`.`appointment` WHERE `ssn`=" + "'"
				+ ssn + "' AND '" + "`date`='" + date +"' LIMIT 0 , 30");

		// Creating a variable to execute query
		final ResultSet result = statement.executeQuery();
		// System.out.println(result);
		// constructs a new empty ArrayList

		final List<Appointment> appInfo = new ArrayList<Appointment>();

		while (result.next()) {
			Appointment tempApp = createAppointment(result.getString(6));
			appInfo.add(tempApp);
			/*
			 * Below are appointment attributes. 1 = ssn 5 = reason 2 = date 6 =
			 * name 3 = timeStart 7 = timeEnd 4 = doctor 8 = patient
			 */
		}
		return appInfo;
	}

	/**
	 * ------------------------------------------------------------------------
	 * -----------------------------Need to be tested for Time Use this method
	 * ONLY if you're accessing an attribute with a TIME DATATYPE.
	 * 
	 * @param ssn
	 * @param attributeNumber
	 * @return
	 * @throws Exception
	 */
	private Time getAppTime(int ssn, int attributeNumber) throws Exception {
		// System.out.println("Ran getAppTime");
		// create the query
		final PreparedStatement statement = connect().prepareStatement(
				"SELECT * FROM appointment WHERE ssn=" + ssn);

		// Creating a variable to execute query
		final ResultSet result = statement.executeQuery();

		Time time = null;

		while (result.next()) {
			switch (result.getMetaData().getColumnType(attributeNumber)) {
			case java.sql.Types.TIME:
				time = result.getTime(attributeNumber);
				break;
			default:
				time = null;
				break;
			}

		}
		return time;
	}

	/**
	 * ------------------------------------------------------------------------
	 * -----------------------------Need to be tested Create one appointment
	 * instance for you
	 * 
	 * @param username
	 * @param date
	 * @param user
	 * @return
	 * @throws Exception
	 */
	private Appointment createAppointment(String username) 
	{
		try{
			// System.out.println("Ran createAppointment");

			int tempSSN = 0;

			tempSSN = getSSN(username);

			final Date a2 = getAppDate(tempSSN, 2); /* date */
			final Time a3 = getAppTime(tempSSN, 3); /* timeStart */
			final String a4 = getAppAttributes(tempSSN, 4); /* doctor */
			final String a5 = getAppAttributes(tempSSN, 5); /* reason */
			final String a6 = getAppAttributes(tempSSN, 6); /* name */
			final Time a7 = getAppTime(tempSSN, 7); /* timeEnd */
			final String a8 = getAppAttributes(tempSSN, 8); /* patient */

			final Appointment app = new Appointment(a2, a3, a7);
			app.setDate(a2);
			app.setTimeStart(a3);
			app.setDoctor(a4);
			app.setReason(a5);
			app.setPatient(a6);
			app.setTimeEnd(a7);
			app.setPatient(a8);

			return app;
		}
		catch (Exception e)
		{
			return null;
		}
	}

	/**
	 * ------------------------------------------------------------------------
	 * -----------------------------Need to be tested for Time Use this method
	 * to add a new appointment. User parameter must have a SSN and Name in the
	 * database. Patient must already have a user profile in the database.
	 * 
	 * @param user
	 *            - user instance
	 * @param app
	 *            - appointment instance
	 * @return boolean - true for success appointment addition to database
	 */
	public boolean addAppointment(int ssn, Appointment app) {
		// System.out.println("Ran addAppointment");
		Statement stmt = null;

		/**/
		final Date appDate = app.getDate();
		// Date appDate = user.getBirthDate();
		final Time timeStart = app.getTimeStart();
		final String doctor = app.getDoctor();
		final String reason = app.getReason();
		final String name = app.getPatient();
		/* String name = "John"; */
		final Time timeEnd = app.getTimeEnd();
		final String patient = app.getPatient();

		/*
		 * System.out.print("INSERT INTO `CS2340`.`appointment` " +
		 * "(`ssn`, `date`, `timeStart`, `doctor`, 
		 * `reason`, `name`, `timeEnd`, `patient`) "
		 * + "VALUES " +
		 * "('"+ssn+"', '"+appDate+"', '"+timeStart+"', '"+doctor+"', '"
		 * +reason+"', '"+name+"', '"+timeEnd+"', '"+patient+"');");
		 */

		try {
			stmt = connect().createStatement();

			stmt.executeUpdate("INSERT INTO `CS2340`.`appointment` "
					+ "(`ssn`, `date`, `timeStart`, `doctor`, `reason`,"
					+ " `name`, `timeEnd`, `patient`) "
					+ "VALUES " + "('" + ssn + "', '" + appDate + "', '"
					+ timeStart + "', '" + doctor + "', '" + reason + "', '"
					+ name + "', '" + timeEnd + "', '" + patient + "');");

			/*
			 * Below are appointment attributes. 1 = ssn 5 = reason 2 = date 6 =
			 * name 3 = timeStart 7 = timeEnd 4 = doctor 8 = patient
			 */
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			e.getClass();
			// System.out.println("SQL Exception in addAppointment");
			return false;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * Simply deletes an appointment, must give date and ssn
	 * 
	 * @param ssn
	 * @param date
	 * @return boolean True if the appointment was deleted
	 * @throws SQLException
	 * @throws Exception
	 */
	public void deleteAppointment(int ssn, Date date) 
	{
		Statement stmt = null;
		try{

			// System.out.println("Ran deleteUser");

			final boolean confirmation = true;

			/*
			 * DELETE FROM `CS2340`.`appointment` WHERE `appointment`.`ssn` =
			 * '111111111' AND `appointment`.`date` = '0000-00-00'
			 */

			stmt = connect().createStatement();
			stmt.executeUpdate("DELETE FROM `CS2340`.`appointment`"
					+ " WHERE `appointment`.`ssn` = '"
					+ ssn + "' AND `appointment`.`date` = '" + date + "'");

			stmt = connect().createStatement();
			stmt.close();
		} 
		catch(SQLException e)
		{
			System.out.println("Delete did not occur");
		}

	}



	/**
	 * Update an appointment using a user and the appointment instance
	 * 
	 * @param user
	 * @param app
	 * @return boolean - true if it worked
	 * @throws SQLException
	 * @throws Exception
	 */
	public void updateAppointment(int ssn, Appointment app)
	{
		try
		{
			//System.out.println("Ran updateUser");
			Statement stmt = null;

			final Date appDate = app.getDate();
			// Date appDate = user.getBirthDate();
			final Time timeStart = app.getTimeStart();
			final String doctor = app.getDoctor();
			final String reason = app.getReason();
			final String name = app.getPatient();
			/* String name = "John"; */
			final Time timeEnd = app.getTimeEnd();
			final String patient = app.getPatient();


			stmt = connect().createStatement();
			stmt.executeUpdate("UPDATE  `CS2340`.`appointment` SET  "
					+ "`ssn` =  '"
					+ ssn
					+ "', "
					+ "`date` =  '"
					+ appDate
					+ "', "
					+ "`timeStart` =  '"
					+ timeStart
					+ "',"
					+ "`doctor` =  '"
					+ doctor
					+ "',"
					+ "`reason` =  '"
					+ reason
					+ "',"
					+ "`name` =  '"
					+ name
					+ "',"
					+ "`timeEnd` =  '"
					+ timeEnd
					+ "',"
					+ "`patient` =  '"
					+ patient
					+ "',"
					+ "WHERE `appointment`.`ssn` = "
					+ ssn
					+ "AND `appointment`.`date` = " + appDate);


			stmt = connect().createStatement();
			stmt.close();
		}
		catch(Exception e)
		{
			System.out.println("updateAppointment failed");
		}


	}
}

